$(function () {
  $('.gozhuce').on('click', function () {
    $('.zhuce').show()
    $('.denglu').hide()
  })

  $('.godenglu').on('click', function () {
    $('.denglu').show()
    $('.zhuce').hide()
  })

  //表单验证
  var form = layui.form
  var layer = layui.layer
  console.log(layui)
  form.verify({
    password: (value) => {
      if (!/^[\S]{6,12}$/.test(value)) {
        return '密码必须6到12位，且不能出现空格'
      }
    },

    Pwd: (value) => {
      var text = $('.oldmima').val()
      if (text != value) {
        return '两次密码不一致'
      }
    }
  })

  //登录账号
  $('form.denglu').on('submit', function (e) {
    e.preventDefault()
    $.ajax({
      url: '/api/login',
      type: 'post',
      data: $('form.denglu').serialize(),
      success: function (res) {
        console.log(res)
        if (res.message == '登录成功！') {
          layer.msg('登录成功')
          location.href = '/index.html'
          localStorage.setItem('token', res.token)
        }
      }
    })
  })

  //注册账号
  $('form.zhuce').on('submit', function (e) {
    e.preventDefault()
    $.ajax({
      url: '/api/reguser',
      type: 'post',
      data: $('form.zhuce').serialize(),
      success: function (res) {
        console.log(res)
        if (res.message == '注册成功！') {
          layer.msg('注册成功')
        } else {
          layer.msg('该账号已被注册')
        }
      }
    })
  })
})
